﻿using System;
using System.Linq;
using System.Web.Mvc;
using GsnHelper;
using ZhongheCms.Models;
namespace ZhongheCms.Controllers
{
    public class JobInfoController : Controller
    {
        //
        // GET: /JobInfo/
        //职位管理
        public ActionResult Index(int id = 0,int pageIndex=1, string m = "", string f = "", string p = "", string fid = "", string r = "", string sdate = "", string edate = "", string jname = "")
        {
            ViewBag.m = m;
            ViewBag.f = f;
            ViewBag.p = p;
            ViewBag.fid = fid;
            ViewBag.r = r;
            ViewBag.sdate = sdate;
            ViewBag.edate = edate;
            ViewBag.jname = jname;
            ViewBag.pageIndex = pageIndex;
            if (GswTblUser.CurrentUser == null)
                return RedirectToAction("Login", "Account");
            GswTblUser.CurrentUser.ModuleName = m;
            GswTblUser.CurrentUser.FuncName = f;
            GswTblJobInfo.Update("set JobState=2 where EndDate<'" + DateTime.Today.ToShortDateString() + "'");
            var qry = GswTblJobInfo.GetJobInfo(sdate, edate, jname, GswTblUser.CurrentUser.CustomerId,0,pageIndex);

            return View(qry);
        }
        public ActionResult Create(int id = 0, int pageIndex = 1, string m = "", string f = "", string p = "", string fid = "", string sdate = "", string edate = "", string jname = "")
        {
            ViewBag.m = m;
            ViewBag.f = f;
            ViewBag.p = p;
            ViewBag.fid = fid;
            ViewBag.sdate = sdate;
            ViewBag.edate = edate;
            ViewBag.jname = jname;
            ViewBag.pageIndex = pageIndex;
            if (GswTblUser.CurrentUser == null)
                return RedirectToAction("Login", "Account");
            GswTblUser.CurrentUser.ModuleName = m;
            GswTblUser.CurrentUser.FuncName = f;
            var jobInfo = id == 0 ? new GswTblJobInfo { PubDate = DateTime.Today, EndDate = DateTime.Today, ParentId = GswTblUser.CurrentUser.CustomerId } : GswTblJobInfo.SingleOrDefault(id);
            jobInfo.BaseInfos = GswTblBaseInfo.Fetch("where CustomerId=" + GswTblUser.CurrentUser.CustomerId +
                                     " and (FunctionId=59  or FunctionId=60 or FunctionId=66 or FunctionId=67 or FunctionId=68 ) order by FunctionName,TypeNo");
            jobInfo.ProTypes = GswTblJobInfo.GetJobTypes(GswTblUser.CurrentUser.CustomerId);
            return View(jobInfo);
        }
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Create(FormCollection c, int id = 0, int pageIndex = 1, string m = "", string f = "", string p = "", string fid = "", string sdate = "", string edate = "", string jname = "")
        {
            ViewBag.m = m;
            ViewBag.f = f;
            ViewBag.p = p;
            ViewBag.fid = fid;
            ViewBag.sdate = sdate;
            ViewBag.edate = edate;
            ViewBag.jname = jname;
            ViewBag.pageIndex = pageIndex;
            if (GswTblUser.CurrentUser == null)
                return RedirectToAction("Login", "Account");
            GswTblUser.CurrentUser.ModuleName = m;
            GswTblUser.CurrentUser.FuncName = f;
            if (GswTblJobInfo.SingleOrDefault("where  ParentId =" + GswTblUser.CurrentUser.CustomerId + " and JobNo='" + c["JobNo"] + "' and Id<>" + id + "") != null)
                ModelState.AddModelError("", "职位编号" + c["JobNo"].Trim() + "已经存在");
            if (string.IsNullOrEmpty(c["CusShortName"]))
                ModelState.AddModelError("", "招聘单不允许为空");
            if (InputValid.StrToInt(c["JobTypeId"], 0) == 0)
                ModelState.AddModelError("", "请选择职位类别");
            var jobInfo = id == 0 ? new GswTblJobInfo() : GswTblJobInfo.SingleOrDefault(id);

            var baseInfos = GswTblBaseInfo.Fetch("where CustomerId=" + GswTblUser.CurrentUser.CustomerId +
                                     "  and (FunctionId=59  or FunctionId=60 or FunctionId=66 or FunctionId=67 or FunctionId=68 ) order by FunctionName,TypeNo");
            jobInfo.PubDate = DateTime.Parse(c["PubDate"]);
            jobInfo.EndDate = DateTime.Parse(c["EndDate"]);
            //jobInfo.JobNo = c["JobNo"];
            jobInfo.JobTitle = c["JobTitle"];
            jobInfo.JobNum = InputValid.StrToInt(c["JobNum"], 0);
            jobInfo.WorkDate = c["WorkDate"];
            jobInfo.WorkTime = c["WorkTime"];
            jobInfo.Workplace = c["Workplace"];
            jobInfo.Salary = c["Salary"];
            jobInfo.CustomerId = 0;
            //if (InputValid.StrToInt(c["CustomerId"], 0) > 0)
            //{
            //    var cusinfo = GswTblCustomer.SingleOrDefault(InputValid.StrToInt(c["CustomerId"], 0));
            //    jobInfo.CusShortName = cusinfo.CusShortName;
            //}
            //else
            //{
            //    jobInfo.CusShortName = "";
            //}
            jobInfo.CusShortName = c["CusShortName"];
            jobInfo.CustomerAlias = c["CustomerAlias"];
            jobInfo.JobTypeId = InputValid.StrToInt(c["JobTypeId"], 0);
            jobInfo.JobTypeName = GswTblBaseInfo.GetMasterTypeName(baseInfos,
                InputValid.StrToInt(c["JobTypeId"], 0));
            jobInfo.WorkTypeId = InputValid.StrToInt(c["WorkTypeId"], 0);
            jobInfo.WorkTypeName = GswTblBaseInfo.GetMasterTypeName(baseInfos,
                InputValid.StrToInt(c["WorkTypeId"], 0));
            jobInfo.PayTypeId = InputValid.StrToInt(c["PayTypeId"], 0);
            jobInfo.PayTypeName = GswTblBaseInfo.GetMasterTypeName(baseInfos,
                InputValid.StrToInt(c["PayTypeId"], 0));
            jobInfo.BodyLengthId = InputValid.StrToInt(c["BodyLengthId"], 0);
            jobInfo.BodyLength = GswTblBaseInfo.GetMasterTypeName(baseInfos,
                InputValid.StrToInt(c["BodyLengthId"], 0));

            jobInfo.GenderId = InputValid.StrToInt(c["GenderId"], 0);
            jobInfo.GenderName = GswTblBaseInfo.GetMasterTypeName(baseInfos,
                InputValid.StrToInt(c["GenderId"], 0));
            jobInfo.RegRankId = 0;
            jobInfo.RegRank = c["RegRank"];
            jobInfo.JobInterview = c["JobInterview"];
            jobInfo.JobDesc = c["JobDesc"];
            jobInfo.ParentId = GswTblUser.CurrentUser.CustomerId;
            jobInfo.JobPic = c["JobPic"];
            jobInfo.ReFlag = InputValid.StrToInt(c["ReFlag"], 0);
            jobInfo.HotFlag = InputValid.StrToInt(c["HotFlag"], 0);
            //jobInfo.PubTime = DateTime.Now.ToShortTimeString();
            jobInfo.SourceFlag = 0;
            try
            {
                if (ModelState.IsValid)
                {

                    if (id == 0)
                    {
                       
                        if (string.IsNullOrEmpty(c["JobNo"]))
                        {
                            int sno = GswTblSno.GetSnoByMonth("job");
                            string jobno = "P" + InputValid.Right(DateTime.Today.Year.ToString(), 2) +
                                           DateTime.Today.Month.ToString("D2") + DateTime.Today.Day.ToString("D2") +
                                           sno.ToString("D2");
                            GswTblSno.Update("set sno=@0,curyear=@1,curmonth=@2 where keycode=@3", sno+1, DateTime.Today.Year, DateTime.Today.Month, "job");
                            jobInfo.JobNo = jobno;

                        }
                        else
                        {
                            jobInfo.JobNo = c["JobNo"];
                        }
                        jobInfo.PubTime = DateTime.Now.ToShortTimeString();
                        jobInfo.Insert();
                        jobInfo = new GswTblJobInfo { PubDate = DateTime.Today, EndDate = DateTime.Today, ParentId = GswTblUser.CurrentUser.CustomerId };

                    }
                    else
                    {
                        jobInfo.Update();
                    }
                    ViewBag.scode = "1";
                }

            }
            catch (Exception e)
            {
                ModelState.AddModelError(string.Empty, e.Message);
            }
            if (!ModelState.IsValid)
                ViewBag.scode = "0";
            jobInfo.BaseInfos = baseInfos;
            jobInfo.ProTypes = GswTblJobInfo.GetJobTypes(GswTblUser.CurrentUser.CustomerId);
            return View(jobInfo);

        }

        public ActionResult Delete(int id, int pageIndex = 1, string m = "", string f = "", string p = "", string fid = "", string sdate = "", string edate = "", string jname = "")
        {
            ViewBag.m = m;
            ViewBag.f = f;
            ViewBag.p = p;
            ViewBag.fid = fid;
            ViewBag.sdate = sdate;
            ViewBag.edate = edate;
            ViewBag.jname = jname;
            ViewBag.pageIndex = pageIndex;
            if (GswTblUser.CurrentUser == null)
                return RedirectToAction("Login", "Account");
            GswTblUser.CurrentUser.ModuleName = m;
            GswTblUser.CurrentUser.FuncName = f;
            GswTblJobInfo.Update("set DelFlag=1 where id=" + id);
            var qry = GswTblJobInfo.GetJobInfo(sdate, edate, jname, GswTblUser.CurrentUser.CustomerId,0,pageIndex);
            return View("Index", qry);
        }
        public ActionResult JobStop(int id, int pageIndex = 1, string m = "", string f = "", string p = "", string fid = "", string sdate = "", string edate = "", string jname = "", int scode = 0)
        {
            ViewBag.m = m;
            ViewBag.f = f;
            ViewBag.p = p;
            ViewBag.fid = fid;
            ViewBag.sdate = sdate;
            ViewBag.edate = edate;
            ViewBag.jname = jname;
            if (GswTblUser.CurrentUser == null)
                return RedirectToAction("Login", "Account");
            GswTblUser.CurrentUser.ModuleName = m;
            GswTblUser.CurrentUser.FuncName = f;
            int jcode = scode == 1 ? 2 : 1;
            GswTblJobInfo.Update("set JobState="+jcode+" where id=" + id);
            var qry = GswTblJobInfo.GetJobInfo(sdate, edate, jname, GswTblUser.CurrentUser.CustomerId,0,pageIndex);
            return View("Index", qry);
        }
        //发布审核
        public ActionResult PubAudit(int id = 0,int pageIndex=1, string m = "", string f = "", string p = "", string fid = "", string r = "", string sdate = "", string edate = "", string jname = "")
        {
            ViewBag.m = m;
            ViewBag.f = f;
            ViewBag.p = p;
            ViewBag.fid = fid;
            ViewBag.r = r;
            ViewBag.sdate = sdate;
            ViewBag.edate = edate;
            ViewBag.jname = jname;
            ViewBag.pageIndex = pageIndex;
            if (GswTblUser.CurrentUser == null)
                return RedirectToAction("Login", "Account");
            GswTblUser.CurrentUser.ModuleName = m;
            GswTblUser.CurrentUser.FuncName = f;
            var qry = GswTblJobInfo.GetJobInfoAudit(sdate, edate, jname, GswTblUser.CurrentUser.CustomerId,pageIndex);
            return View(qry);
        }

        public ActionResult PubAuditView(int id = 0,int pageIndex=1, string m = "", string f = "", string p = "", string fid = "", string sdate = "", string edate = "", string jname = "")
        {
            ViewBag.m = m;
            ViewBag.f = f;
            ViewBag.p = p;
            ViewBag.fid = fid;
            ViewBag.sdate = sdate;
            ViewBag.edate = edate;
            ViewBag.jname = jname;
            ViewBag.pageIndex = pageIndex;
            if (GswTblUser.CurrentUser == null)
                return RedirectToAction("Login", "Account");
            GswTblUser.CurrentUser.ModuleName = m;
            GswTblUser.CurrentUser.FuncName = f;
            var jobInfo = GswTblJobInfo.SingleOrDefault(id);
            jobInfo.BaseInfos = GswTblBaseInfo.Fetch("where CustomerId=" + GswTblUser.CurrentUser.CustomerId +
                                     " order by FunctionName,TypeNo");
            return View(jobInfo);
        }
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult PubAuditView(FormCollection c, int id = 0,int pageIndex=1, string m = "", string f = "", string p = "", string fid = "", string sdate = "", string edate = "", string jname = "")
        {
            ViewBag.m = m;
            ViewBag.f = f;
            ViewBag.p = p;
            ViewBag.fid = fid;
            ViewBag.sdate = sdate;
            ViewBag.edate = edate;
            ViewBag.jname = jname;
            ViewBag.pageIndex = pageIndex;
            if (GswTblUser.CurrentUser == null)
                return RedirectToAction("Login", "Account");
            GswTblUser.CurrentUser.ModuleName = m;
            GswTblUser.CurrentUser.FuncName = f;
            if (GswTblJobInfo.SingleOrDefault("where  ParentId =" + GswTblUser.CurrentUser.CustomerId + " and JobNo='" + c["JobNo"] + "' and Id<>" + id + "") != null)
                ModelState.AddModelError("", "职位编号" + c["JobNo"].Trim() + "已经存在");
            if (string.IsNullOrEmpty(c["CusShortName"]))
                ModelState.AddModelError("", "招聘单不允许为空");

            var jobInfo = GswTblJobInfo.SingleOrDefault(id);

            var baseInfos = GswTblBaseInfo.Fetch("where CustomerId=" + GswTblUser.CurrentUser.CustomerId +
                                     " order by FunctionName,TypeNo");
            jobInfo.PubDate = DateTime.Parse(c["PubDate"]);
            jobInfo.EndDate = DateTime.Parse(c["EndDate"]);
            jobInfo.JobNo = c["JobNo"];
            jobInfo.JobTitle = c["JobTitle"];
            jobInfo.JobNum = InputValid.StrToInt(c["JobNum"], 0);
            jobInfo.WorkDate = c["WorkDate"];
            jobInfo.WorkTime = c["WorkTime"];
            jobInfo.Workplace = c["Workplace"];
            jobInfo.Salary = c["Salary"];
            //jobInfo.CustomerId = InputValid.StrToInt(c["CustomerId"], 0);
            jobInfo.CusShortName = c["CusShortName"];
            jobInfo.CustomerAlias = c["CustomerAlias"];
            jobInfo.JobTypeId = InputValid.StrToInt(c["JobTypeId"], 0);
            jobInfo.JobTypeName = GswTblBaseInfo.GetMasterTypeName(baseInfos,
                InputValid.StrToInt(c["JobTypeId"], 0));
            jobInfo.WorkTypeId = InputValid.StrToInt(c["WorkTypeId"], 0);
            jobInfo.WorkTypeName = GswTblBaseInfo.GetMasterTypeName(baseInfos,
                InputValid.StrToInt(c["WorkTypeId"], 0));
            jobInfo.PayTypeId = InputValid.StrToInt(c["PayTypeId"], 0);
            jobInfo.PayTypeName = GswTblBaseInfo.GetMasterTypeName(baseInfos,
                InputValid.StrToInt(c["PayTypeId"], 0));
            jobInfo.BodyLengthId = InputValid.StrToInt(c["BodyLengthId"], 0);
            jobInfo.BodyLength = GswTblBaseInfo.GetMasterTypeName(baseInfos,
                InputValid.StrToInt(c["BodyLengthId"], 0));

            jobInfo.GenderId = InputValid.StrToInt(c["GenderId"], 0);
            jobInfo.GenderName = GswTblBaseInfo.GetMasterTypeName(baseInfos,
                InputValid.StrToInt(c["GenderId"], 0));
            jobInfo.RegRankId = 0;
            jobInfo.RegRank = c["RegRank"];
            jobInfo.JobInterview = c["JobInterview"];
            jobInfo.JobDesc = c["JobDesc"];
            jobInfo.ParentId = GswTblUser.CurrentUser.CustomerId;
            jobInfo.JobPic = c["JobPic"];
            jobInfo.ReFlag = InputValid.StrToInt(c["ReFlag"], 0);
            jobInfo.HotFlag = InputValid.StrToInt(c["HotFlag"], 0);
            jobInfo.JobState = InputValid.StrToInt(c["JobState"], 0);
            try
            {
                if (ModelState.IsValid)
                {

                    jobInfo.PubTime = DateTime.Now.ToShortTimeString();
                    jobInfo.Update();
                    ViewBag.scode = "1";
                }

            }
            catch (Exception e)
            {
                ModelState.AddModelError(string.Empty, e.Message);
            }
            if (!ModelState.IsValid)
                ViewBag.scode = "0";
            jobInfo.BaseInfos = baseInfos;
            return View(jobInfo);

        }
        /// <summary>
        /// 报名管理
        /// </summary>

        public ActionResult WorkList(int id = 0,int pageIndex=1, string m = "", string f = "", string p = "", string fid = "", string r = "", string sdate = "", string edate = "", string jname = "")
        {
            ViewBag.m = m;
            ViewBag.f = f;
            ViewBag.p = p;
            ViewBag.fid = fid;
            ViewBag.r = r;
            ViewBag.sdate = sdate;
            ViewBag.edate = edate;
            ViewBag.jname = jname;
            ViewBag.pageIndex = pageIndex;
            if (GswTblUser.CurrentUser == null)
                return RedirectToAction("Login", "Account");
            GswTblUser.CurrentUser.ModuleName = m;
            GswTblUser.CurrentUser.FuncName = f;
            var qry = GswTblJobInfo.GetJobInfoWork(sdate, edate, jname, GswTblUser.CurrentUser.CustomerId,pageIndex);
            return View(qry);
        }
        public ActionResult WorkManager(int id = 0,int pageIndex=1, string m = "", string f = "", string p = "", string fid = "", string r = "", string sdate = "", string edate = "", string jname = "")
        {
            ViewBag.m = m;
            ViewBag.f = f;
            ViewBag.p = p;
            ViewBag.fid = fid;
            ViewBag.r = r;
            ViewBag.sdate = sdate;
            ViewBag.edate = edate;
            ViewBag.jname = jname;
            ViewBag.pageIndex = pageIndex;
            if (GswTblUser.CurrentUser == null)
                return RedirectToAction("Login", "Account");
            GswTblUser.CurrentUser.ModuleName = m;
            GswTblUser.CurrentUser.FuncName = f;
            var jobInfo = GswTblJobInfo.SingleOrDefault(id);
            jobInfo.JobWorks = GswViewJobWork.Fetch("where JobId=" + id +
                                     " order by id");
            return View(jobInfo);

        }
        [HttpPost]
        public ActionResult WorkManager(int id, FormCollection c, int pageIndex = 1, string m = "", string f = "", string p = "", string fid = "", string r = "", string sdate = "", string edate = "", string jname = "")
        {
            ViewBag.m = m;
            ViewBag.f = f;
            ViewBag.p = p;
            ViewBag.fid = fid;
            ViewBag.r = r;
            ViewBag.sdate = sdate;
            ViewBag.edate = edate;
            ViewBag.jname = jname;
            ViewBag.pageIndex = pageIndex;
            if (GswTblUser.CurrentUser == null)
                return RedirectToAction("Login", "Account");
            GswTblUser.CurrentUser.ModuleName = m;
            GswTblUser.CurrentUser.FuncName = f;
            var jobInfo = GswTblJobInfo.SingleOrDefault(id);
            var worklist = GswViewJobWork.Fetch("where JobId=" + id +
                                     " order by id");
            int flag = 0;
            if (worklist.Any(item => InputValid.StrToInt(c["chk" + item.Id], 0) > 0))
                flag = 1;
            if (flag == 0)
                ModelState.AddModelError("", "请选择要录取的学员");
            jobInfo.AcceptedNotice = c["AcceptedNotice"];
            if (jobInfo.AcceptedDate == null)
                jobInfo.AcceptedDate = DateTime.Today;
            int num = 0;
            string mydate = "";
            try
            {
                foreach (var item in worklist)
                {
                    if (InputValid.StrToInt(c["chk" + item.Id], 0) > 0)
                    {
                        mydate = c["a" + item.Id];
                        if (string.IsNullOrEmpty(mydate))
                            mydate = DateTime.Today.ToShortDateString();
                        GswTblJobWork.Update("set AcceptedFlag=1,AcceptedDate='"+mydate+"' where id=" + item.Id);
                        num++;
                    }
                    else
                    {
                        GswTblJobWork.Update("set AcceptedFlag=0,,AcceptedDate='' where id=" + item.Id);
                    }
                }
                jobInfo.AcceptedNum = num;
                jobInfo.Update();
                //GswTblJobInfo.Update("set AcceptedNum=" + num + " where id=" + id);
                ViewBag.scode = "1";
            }
            catch (Exception e)
            {
                ModelState.AddModelError("", e.Message);

            }
            if (!ModelState.IsValid)
                ViewBag.scode = "0";
            jobInfo.JobWorks = GswViewJobWork.Fetch("where JobId=" + id +
                                     " order by id");
            return View(jobInfo);

        }


        public ActionResult PayList(int id = 0,int pageIndex=1, string m = "", string f = "", string p = "", string fid = "", string r = "", string sdate = "", string edate = "", string jname = "")
        {
            ViewBag.m = m;
            ViewBag.f = f;
            ViewBag.p = p;
            ViewBag.fid = fid;
            ViewBag.r = r;
            ViewBag.sdate = sdate;
            ViewBag.edate = edate;
            ViewBag.jname = jname;
            ViewBag.pageIndex = pageIndex;
            if (GswTblUser.CurrentUser == null)
                return RedirectToAction("Login", "Account");
            GswTblUser.CurrentUser.ModuleName = m;
            GswTblUser.CurrentUser.FuncName = f;
            var qry = GswTblJobInfo.GetJobInfoPayNotice(sdate, edate, jname, GswTblUser.CurrentUser.CustomerId,pageIndex);
            return View(qry);
        }

        public ActionResult PayNotice(int id = 0,int pageIndex=1, string m = "", string f = "", string p = "", string fid = "", string r = "", string sdate = "", string edate = "", string jname = "")
        {
            ViewBag.m = m;
            ViewBag.f = f;
            ViewBag.p = p;
            ViewBag.fid = fid;
            ViewBag.r = r;
            ViewBag.sdate = sdate;
            ViewBag.edate = edate;
            ViewBag.jname = jname;
            ViewBag.pageIndex = pageIndex;
            if (GswTblUser.CurrentUser == null)
                return RedirectToAction("Login", "Account");
            GswTblUser.CurrentUser.ModuleName = m;
            GswTblUser.CurrentUser.FuncName = f;
            var jobInfo = GswTblJobInfo.SingleOrDefault(id);
            if (jobInfo.PayNoticeDate == null)
                jobInfo.PayNoticeDate = DateTime.Today;
            jobInfo.JobWorkAlls = GswViewJobWorkAll.Fetch("where JobId=" + id + " order by MemberName");
            return View(jobInfo);

        }
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult PayNotice(int id, FormCollection c,int pageIndex=1, string m = "", string f = "", string p = "", string fid = "", string r = "", string sdate = "", string edate = "", string jname = "")
        {
            ViewBag.m = m;
            ViewBag.f = f;
            ViewBag.p = p;
            ViewBag.fid = fid;
            ViewBag.r = r;
            ViewBag.sdate = sdate;
            ViewBag.edate = edate;
            ViewBag.jname = jname;
            ViewBag.pageIndex = pageIndex;
            if (GswTblUser.CurrentUser == null)
                return RedirectToAction("Login", "Account");
            GswTblUser.CurrentUser.ModuleName = m;
            GswTblUser.CurrentUser.FuncName = f;
            var jobInfo = GswTblJobInfo.SingleOrDefault(id);
            if (string.IsNullOrEmpty(c["PayNotice"]))
                ModelState.AddModelError("", "通知内容不允许为空");
            jobInfo.PayNoticeFlag = 1;
            jobInfo.PayNoticeDate = DateTime.Parse(c["PayNoticeDate"]);
            jobInfo.PayNotice = c["PayNotice"];
            var qry = GswViewJobWorkAll.Fetch("where JobId=" + id + " order by MemberName");

            try
            {

               // GswTblJobInfo.Update("set PayNoticeFlag=1,PayNoticeDate='" + c["PayNoticeDate"] + "' , PayNotice='" + c["PayNotice"] + "' where id=" + id);
                if (ModelState.IsValid)
                {
                    foreach (var item in qry)
                    {
                        GswTblJobWork.Update("set WorkFlag=" + InputValid.StrToInt(c["s" + item.Id], 0) +
                                             ",WorkResult='" + c["t" + item.Id] + "' where id=" + item.Id);
                    }
                    jobInfo.Update();
                    ViewBag.scode = "1";
                }
                
            }
            catch (Exception e)
            {
                ModelState.AddModelError("", e.Message);

            }
            if (!ModelState.IsValid)
                ViewBag.scode = "0";
            jobInfo.JobWorkAlls = GswViewJobWorkAll.Fetch("where JobId=" + id + " order by MemberName");
            return View(jobInfo);

        }

        //Punish
        public ActionResult Punish(int id = 0,int pageIndex=1, string m = "", string f = "", string p = "", string fid = "", string r = "", string sdate = "", string edate = "", string jname = "")
        {
            ViewBag.m = m;
            ViewBag.f = f;
            ViewBag.p = p;
            ViewBag.fid = fid;
            ViewBag.r = r;
            ViewBag.sdate = sdate;
            ViewBag.edate = edate;
            ViewBag.jname = jname;
            ViewBag.pageIndex = pageIndex;
            if (GswTblUser.CurrentUser == null)
                return RedirectToAction("Login", "Account");
            GswTblUser.CurrentUser.ModuleName = m;
            GswTblUser.CurrentUser.FuncName = f;
            var qry = GswTblJobInfo.GetJobInfoWork(sdate, edate, jname, GswTblUser.CurrentUser.CustomerId,pageIndex);
            return View(qry);
        }

        public ActionResult PunishManager(int id = 0, int pageIndex=1,string m = "", string f = "", string p = "", string fid = "", string r = "", string sdate = "", string edate = "", string jname = "")
        {
            ViewBag.m = m;
            ViewBag.f = f;
            ViewBag.p = p;
            ViewBag.fid = fid;
            ViewBag.r = r;
            ViewBag.sdate = sdate;
            ViewBag.edate = edate;
            ViewBag.jname = jname;
            ViewBag.pageIndex = pageIndex;
            if (GswTblUser.CurrentUser == null)
                return RedirectToAction("Login", "Account");
            GswTblUser.CurrentUser.ModuleName = m;
            GswTblUser.CurrentUser.FuncName = f;
            var jobInfo = GswTblJobInfo.SingleOrDefault(id);
            jobInfo.JobWorks = GswViewJobWork.Fetch("where JobId=" + id +
                                     " order by id");
            return View(jobInfo);

        }
        [HttpPost]
        public ActionResult PunishManager(int id, FormCollection c, string m = "", string f = "", string p = "", string fid = "", string r = "", string sdate = "", string edate = "", string jname = "")
        {
            ViewBag.m = m;
            ViewBag.f = f;
            ViewBag.p = p;
            ViewBag.fid = fid;
            ViewBag.r = r;
            ViewBag.sdate = sdate;
            ViewBag.edate = edate;
            ViewBag.jname = jname;
            if (GswTblUser.CurrentUser == null)
                return RedirectToAction("Login", "Account");
            GswTblUser.CurrentUser.ModuleName = m;
            GswTblUser.CurrentUser.FuncName = f;
            var jobInfo = GswTblJobInfo.SingleOrDefault(id);
            var worklist = GswViewJobWork.Fetch("where JobId=" + id +
                                     " order by id");


            try
            {
                foreach (var item in worklist)

                    GswTblJobWork.Update("set WorkFlag=1,WorkType='" + c["w" + item.Id] + "',WorkResult='" +
                                         c["r" + item.Id] + "' where id=" + item.Id);

                ViewBag.scode = "1";
            }
            catch (Exception e)
            {
                ModelState.AddModelError("", e.Message);

            }
            if (!ModelState.IsValid)
                ViewBag.scode = "0";
            jobInfo.JobWorks = GswViewJobWork.Fetch("where JobId=" + id +
                                     " order by id");
            return View(jobInfo);

        }
    }
}
